//
//  VideoListViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "VideoListViewController.h"
#import "KrVideoPlayerController.h"
#import "HTTPMethod.h"
#import "changeString.h"
#import "FitHeader.h"
#import "CYVideoStepsTableViewCell.h"
@interface VideoListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) KrVideoPlayerController  *videoController;
@property(nonatomic, strong)NSString *videoString;
@property(nonatomic, strong)NSMutableArray *stepArray;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UITableView *tableView;

@end

@implementation VideoListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = YES;
    self.title = @"视频";
}

-(void)getData
{
    _stepArray = [NSMutableArray array];
    NSString *PostString = @"http://api.haodou.com/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=43a22149ef21a23a46bffa46f6469ea7&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7C7C4F45AC-D834-40CC-8E57-1C73070C0250%7C28F136CD-C3C2-4542-AF9B-2BAD4F1067E6&format=json&loguid=&method=Info.getVideoUrl&nonce=1446880575&sessionid=1446821118&signmethod=md5&timestamp=1446880575&uuid=8aa885128b339761cd4b5fc380d868d6&v=2&vc=43&vn=v5.4.0";
    NSString *bodyString = [NSString stringWithFormat:@"rid=%@&sign=&uid=&uuid=8aa885128b339761cd4b5fc380d868d6", _IdString];
    [HTTPMethod getDataByString:PostString BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        
        NSDictionary *dic = [data objectForKey:@"result"];
        _videoString = [dic objectForKey:@"Url"];
        _stepArray = [dic objectForKey:@"Steps"];
       
        [self playVideo];
        [_tableView reloadData];
    }];
}

-(void)loadView
{
    [super loadView];
    [self getData];
    [self playVideo];
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 294 * ht, self.view.frame.size.width, 50 * ht)];
    _titleLabel.text = @"步骤";
    _titleLabel.textAlignment = 1;
    _titleLabel.textColor = [UIColor brownColor];
    _titleLabel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_titleLabel];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _titleLabel.frame.origin.y + _titleLabel.frame.size.height, self.view.frame.size.width, 274 * ht) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [[UIView alloc]init];
    
    [self.view addSubview:_tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _stepArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * ht;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    CYVideoStepsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[CYVideoStepsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    cell.infoLabel.text = [_stepArray[indexPath.row] valueForKey:@"Content"];
    return cell;
}

//开始播放
- (void)playVideo
{
    NSURL *url = [NSURL URLWithString:_videoString];
    [self addVideoPlayerWithURL:url];
}

- (void)addVideoPlayerWithURL:(NSURL *)url
{
    if (!self.videoController) {
        
        self.videoController = [[KrVideoPlayerController alloc] initWithFrame:CGRectMake(0, 64 * ht, self.view.frame.size.width, 230 * ht)];
        
        __weak typeof(self)weakSelf = self;
        [self.videoController setDimissCompleteBlock:^{
            weakSelf.videoController = nil;
        }];
        
        [self.videoController setWillChangeToFullscreenMode:^{
            [weakSelf toolbarHidden:YES];
        }];
        
        [self.videoController setWillBackOrientationPortrait:^{
            [weakSelf toolbarHidden:NO];
            
        }];
        [self.view addSubview:self.videoController.view];
    }
    self.videoController.contentURL = url;
}

- (void)toolbarHidden:(BOOL)Bool
{
    self.navigationController.navigationBar.hidden = Bool;
    self.tabBarController.tabBar.hidden = Bool;
    _titleLabel.hidden = Bool;
    _tableView.hidden = Bool;
    [[UIApplication sharedApplication] setStatusBarHidden:Bool withAnimation:UIStatusBarAnimationFade];
}

//退出界面停止播放
-(void)viewWillDisappear:(BOOL)animated
{
    [_videoController pause];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
